package com.app.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.app.domain.UploadParameter;
import com.app.domain.YjData;
import com.app.domain.hsy.ResponseResult;
import com.app.domain.hsy.Yjsy_M;
import com.app.domain.hsy.Yjsy_S;
import com.app.domain.hsy.Yjsy_S_S;
import com.app.domain.xygs.YajiangData;
import com.app.domain.xygs.YajiangDetailData;
import com.app.utils.HttpClients;
import com.app.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Encoder;

import java.util.*;

@Service
@Slf4j
public class XygsService {

    @Autowired
    UploadParameterService uploadParameterService;
    @Autowired
    YjDataService yjDataService;


    public List<YajiangData> getYajiangData(YjData yjData, UploadParameter uploadParameter, String sbbh) {
        YajiangData yjsy_m = new YajiangData();
        List<YajiangData> yjsyMList = new ArrayList<>();

        String uuid = UUID.randomUUID().toString().replace("-", "");
        yjsy_m.setSyjid(uuid);
        yjsy_m.setShebeibianhao(sbbh);
        yjsy_m.setSgdw(uploadParameter.getSgdw());
        yjsy_m.setJldw(uploadParameter.getJldw());
        yjsy_m.setHtbh(uploadParameter.getHtbh());
        yjsy_m.setGcmc(uploadParameter.getGcmc());
        yjsy_m.setYjsj(yjData.getMudjack_date() + " " + yjData.getBegin_time());
        yjsy_m.setZhbw(uploadParameter.getZhbw());
        yjsy_m.setSgbw(uploadParameter.getSgbw());
        yjsy_m.setGjjg(uploadParameter.getGjjg());
        yjsy_m.setGjbh(yjData.getBridge_code());
        yjsy_m.setQw(String.valueOf(yjData.getEnvironment_temperature()));
        yjsy_m.setCjsjl(uploadParameter.getCjsjl());
        yjsy_m.setCpzjl(uploadParameter.getCpzjl());
        yjsy_m.setSw(uploadParameter.getSw());
        yjsy_m.setShuijiaobi(String.valueOf(yjData.getWater_binder_ratio()));
        yjsy_m.setSnyl(uploadParameter.getSnyl());
        yjsy_m.setYjwd(String.valueOf(yjData.getSlurry_temperature()));
        yjsy_m.setMsl(uploadParameter.getMsl());
        yjsy_m.setBeiyong(uploadParameter.getBeiyong());
        yjsy_m.setYjsbbh(yjData.getDevice_code());
        yjsy_m.setLblx(uploadParameter.getLblx());
        yjsy_m.setLianghao(yjData.getBridge_code());
        yjsy_m.setZlsj(uploadParameter.getZlsj());
        yjsy_m.setYajiangji(uploadParameter.getYajiangji());
        yjsy_m.setSnmc(uploadParameter.getSnmc());
        yjsy_m.setKongdaoshu(uploadParameter.getKongdaoshu());
        yjsy_m.setYajiangfangxiang(uploadParameter.getYajiangfangxiang());
        yjsy_m.setYajiangbuzhou(uploadParameter.getYajiangbuzhou());
        yjsy_m.setYajiangguocheng(uploadParameter.getYajiangguocheng());
        yjsy_m.setChushisudu(yjData.getPractical_fluidity());
        yjsy_m.setLiudongdu(yjData.getPractical_fluidity());
        yjsy_m.setMemo(uploadParameter.getMemo());
        yjsy_m.setStatus(Integer.valueOf(uploadParameter.getStatus()));
        yjsyMList.add(yjsy_m);
        return yjsyMList;
    }

    /**
     * 杭绍甬压浆详情表
     *
     * @param yjData
     * @param uploadParameter
     * @param id
     * @return
     */
    public List<YajiangDetailData> getYajiangDetailData(YjData yjData, UploadParameter uploadParameter, String id) {
        List<YajiangDetailData> yjsy_sList = new ArrayList<>();
        YajiangDetailData yjsy_s = new YajiangDetailData();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        yjsy_s.setF_guid(uuid);
        yjsy_s.setSyjid(id);
        yjsy_s.setYajiangsj(yjData.getMudjack_date() + " " + yjData.getBegin_time());
        yjsy_s.setKongdao(yjData.getDuct_code());
        yjsy_s.setYajiangmoshi(uploadParameter.getYajiangmoshi());
        yjsy_s.setPeihebi(uploadParameter.getPhb());
        yjsy_s.setShuijiaobi(String.valueOf(yjData.getWater_binder_ratio()));
        yjsy_s.setJiaobansj(yjData.getPressurize_time());
        yjsy_s.setStarttime(yjData.getMudjack_date() + " " + yjData.getBegin_time());
        yjsy_s.setEndtime(yjData.getMudjack_date() + " " + yjData.getEnd_time());
        yjsy_s.setJinjiangyali(String.valueOf(yjData.getMudjack_pressure()));
        yjsy_s.setFanjiangyali(String.valueOf(yjData.getSlurry_pressure()));
        yjsy_s.setChixushijian(yjData.getPressurize_time());
        yjsy_s.setJinjiangshuliang(uploadParameter.getJinjiangshuliang());
        yjsy_s.setFangjiangliang(uploadParameter.getFanjiangliang());
        yjsy_s.setZkyl("1");
        yjsy_s.setTongguo("");
        yjsy_s.setYjcs("1");
        yjsy_s.setHege("1");
//        yjsy_s.setHoleid(uuid);
        yjsy_s.setStatus(1);
        yjsy_sList.add(yjsy_s);
        return yjsy_sList;
    }


  /*  public List<Yjsy_S_S> getYjsy_S_S(YjData yjData, UploadParameter uploadParameter, String id, String sbbh) {
        List<Yjsy_S_S> yjsy_s_sList = new ArrayList<>();
        Map<String, List> graphData = yjDataService.getGraphData(String.valueOf(yjData.getId()));
        List<String> dateList = graphData.get("dateList");
        List<String> mcgList = graphData.get("mcgList");
        for (int i = 0; i < mcgList.size(); i++) {
            Yjsy_S_S yjsy_s_s = new Yjsy_S_S();
            yjsy_s_s.setSid(UUID.randomUUID().toString().replace("-", ""));
            yjsy_s_s.setShebeibianhao(sbbh);
            yjsy_s_s.setHoleid(id);
            yjsy_s_s.setJlsj(yjData.getMudjack_date() + " " + dateList.get(i));
            yjsy_s_s.setCysj("5");
            yjsy_s_s.setZt("正常");
            yjsy_s_s.setJjyl(mcgList.get(i));
            yjsy_s_s.setFjyl("0");
            yjsy_s_s.setJjl("0");
            yjsy_s_s.setFjl("0");
            yjsy_s_sList.add(yjsy_s_s);
        }
        return yjsy_s_sList;
    }*/

    public <T> ResponseResult sendHsyMessage(String busiType, String serviceUrl, List<T> busiData) {
        Map<String, Object> requestMap = new HashMap();
        requestMap.put("Method", busiType);
        requestMap.put("data", busiData);
        String requestJson = JSON.toJSONString(requestMap);
        log.info("requestJson={}", requestJson);
        ResponseResult responseResult = null;
        try {
            byte[] bytes = requestJson.getBytes("utf-8");
            String base64Str = new BASE64Encoder().encode(bytes);
            String result = HttpClients.requestPost(serviceUrl, base64Str);
            log.info("result={}", result);
            JSONObject metaJson = JSONObject.parseObject(result);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("Method", metaJson.getString("Method"));
            jsonObject.put("succCount", metaJson.getString("succCount"));
            jsonObject.put("sendCount", metaJson.getString("sendCount"));
            result = jsonObject.toString();
            responseResult = (ResponseResult) JSONObject.parseObject(StringUtil.replaceBlank(result), ResponseResult.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return responseResult;
    }
}
